package org.example.controller;

import org.example.api.IUserService;
import org.example.api.entity.User;
import org.example.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: Re
 * @Date: 2021/4/18 14:44
 */
@WebServlet("/delete")
public class DeleteUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;utf-8");
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        IUserService iUserService = new UserServiceImpl();
        User user = (User) session.getAttribute("user");
        if (iUserService.deleteUser(user)){
            req.setAttribute("msg","用户删除成功");
            req.getRequestDispatcher("signIn.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg","用户删除失败");
            req.getRequestDispatcher("success.jsp").forward(req,resp);
        }
    }
}
